
package com.lbd.loan.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.loan.bean.Loan;
import com.lbd.loan.controller.vo.LoanVO;
import com.lbd.loan.service.LoanService;
import com.lbd.util.ExcelExportUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by wang on 2016/9/22.
 */
@RestController
@RequestMapping("/loan")
public class LoanController extends BaseController {

    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    LoanService loanService;

    /**
     * 1. 贷款申请新增
     *
     * @param loan 贷款bean
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result apply(@Valid Loan loan) {
        //贷款申请状态初始化
        loan.setAppStatus(0);
        //执行贷款申请操作，接收贷款申请结果代码
        int applyResult = loanService.loanApply(loan);
        //根据贷款申请执行结果代码，返回执行结果
        /**
         * 0-申请成功
         * 1-申请失败
         * 2-申请重复
         * 其他-申请错误
         * */
        if (applyResult == 0) {
            return new Result(ResultState.SUCCESS, "贷款申请成功！");
        } else {
            if (applyResult == 1) {
                return new Result(ResultState.FAIL, "贷款申请失败！用户 " + loan.getAppName() + " 使用的手机号(" + loan.getAppMobile() +
                        ") 所申请的贷款已受理或受理中，请耐心等待，不要重复提交申请。");
            } else if (applyResult == 2) {
                return new Result(ResultState.FAIL, "贷款申请重复！用户 " + loan.getAppName() + " 使用的手机号(" + loan.getAppMobile() +
                        ") 已经申请贷款，请勿重复提交贷款申请！");
            } else {
                return FAIL;
            }
        }

    }

    /**
     * 2. 贷款申请列表
     *
     * @param page   页码
     * @param rows   行数
     * @param loanVO 贷款bean
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                       @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
                       LoanVO loanVO) {
        //BeanUtils.copyProperties(lo);
        return new Result(ResultState.SUCCESS, "查询成功", loanService.findByPageConditionJpql(page, rows, loanVO));
    }

    /**
     * 3. 贷款申请删除
     *
     * @param ids id数组
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        loanService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");

    }

    /**
     * 4. 贷款申请编辑
     *
     * @param id 贷款id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", loanService.findById(id));

    }

    /**
     * 5. 贷款申请修改
     *
     * @param loan 贷款bean
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Loan loan) {
        loanService.update(loan);
        return new Result(ResultState.SUCCESS, "更新成功");

    }

    /**
     * 6. 贷款申请导出
     *
     * @param request
     * @param response
     * @param loanVO   贷款Vo
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletRequest request, HttpServletResponse response, LoanVO loanVO) {
        List<Loan> list = loanService.findByConditionJpql(loanVO);
        //ExcelExportUtils.exportExcel(request,response,"测试fileName","测试title","测试sheetName",null,Loan.class,list);
        ExcelExportUtils.exportExcel(request, response, DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"), Loan.class, list);
        log.info("文件生成...");
    }

}
